<?php
/**
 * util page
 * @package common_lib_util
 */
/**
 * util page
 * @author pandajingjing
 * @package common_lib_util
 */
class util_page{

	/**
	 * 解析翻页参数
	 * @param int $p_intPageNum 当前页码
	 * @param int $p_intPageSize 每页数量
	 * @param int $p_intTotalCnt 总数量
	 * @return array
	 */
	static function getPage($p_intPageNum,$p_intPageSize,$p_intTotalCnt=0,$p_intDefaultGroupSize=10){
		$arrPage=array( 
				'FIRSTPAGE'=>array( 
						'INDEX'=>1,
						'URL'=>'' 
				),
				'CURRENTPAGE'=>array( 
						'INDEX'=>$p_intPageNum,
						'URL'=>'' 
				),
				'PAGESIZE'=>$p_intPageSize 
		);
		if(is_numeric($p_intPageNum)){
			if($p_intPageNum<1){
				$arrPage['CURRENTPAGE']['INDEX']=1;
			}
		}else{
			$arrPage['CURRENTPAGE']['INDEX']=1;
		}
		if(is_numeric($p_intPageSize)){
			if($p_intPageSize<1){
				$arrPage['PAGESIZE']=20;
			}
		}else{
			$arrPage['PAGESIZE']=20;
		}
		if(-1<$p_intTotalCnt){
			$arrPage['TOTALNUM']=$p_intTotalCnt;
			if(0==$arrPage['TOTALNUM']%$arrPage['PAGESIZE']){
				$arrPage['TOTALPAGE']=array( 
						'INDEX'=>$arrPage['TOTALNUM']/$arrPage['PAGESIZE'],
						'URL'=>'' 
				);
			}else{
				$arrPage['TOTALPAGE']=array( 
						'INDEX'=>floor($arrPage['TOTALNUM']/$arrPage['PAGESIZE'])+1,
						'URL'=>'' 
				);
			}
			if(0==$arrPage['TOTALPAGE']['INDEX']){
				$arrPage['TOTALPAGE']['INDEX']=1;
			}
			if($arrPage['CURRENTPAGE']['INDEX']>$arrPage['TOTALPAGE']['INDEX']){
				$arrPage['CURRENTPAGE']['INDEX']=$arrPage['TOTALPAGE']['INDEX'];
			}
			$arrPage['PREPAGE']=array( 
					'INDEX'=>$arrPage['CURRENTPAGE']['INDEX']-1,
					'URL'=>'' 
			);
			if($arrPage['PREPAGE']['INDEX']<1){
				$arrPage['PREPAGE']['INDEX']=1;
			}
			$arrPage['NEXTPAGE']=array( 
					'INDEX'=>$arrPage['CURRENTPAGE']['INDEX']+1,
					'URL'=>'' 
			);
			if($arrPage['NEXTPAGE']['INDEX']>$arrPage['TOTALPAGE']['INDEX']){
				$arrPage['NEXTPAGE']['INDEX']=$arrPage['TOTALPAGE']['INDEX'];
			}
			if(is_numeric($p_intDefaultGroupSize)){
				if($p_intDefaultGroupSize<1){
					$p_intDefaultGroupSize=20;
				}
			}else{
				$p_intDefaultGroupSize=20;
			}
			$arrPage['GROUPSIZE']=$p_intDefaultGroupSize;
			$arrPage['PREGPAGE']=array( 
					'INDEX'=>$arrPage['CURRENTPAGE']['INDEX']-$p_intDefaultGroupSize,
					'URL'=>'' 
			);
			if($arrPage['PREGPAGE']['INDEX']<1){
				$arrPage['PREGPAGE']['INDEX']=1;
			}
			$arrPage['NEXTGPAGE']=array( 
					'INDEX'=>$arrPage['CURRENTPAGE']['INDEX']+$p_intDefaultGroupSize,
					'URL'=>'' 
			);
			if($arrPage['NEXTGPAGE']['INDEX']>$arrPage['TOTALPAGE']['INDEX']){
				$arrPage['NEXTGPAGE']['INDEX']=$arrPage['TOTALPAGE']['INDEX'];
			}
			$arrPage['NUMURL']=self::_makeNumURL($arrPage['CURRENTPAGE']['INDEX'],$arrPage['TOTALPAGE']['INDEX'],$arrPage['GROUPSIZE']);
		}
		return $arrPage;
	}

	/**
	 * 获取页码数据
	 * @param int $p_intCurrentPage
	 * @param int $p_intTotalPage
	 * @param int $p_intGroupSize
	 * @return array
	 */
	private static function _makeNumURL($p_intCurrentPage,$p_intTotalPage,$p_intGroupSize){
		$intTmp=$p_intGroupSize/2;
		if($p_intGroupSize&1){ //奇数
			$intLeft=floor($intTmp);
			$intRight=$p_intGroupSize-$intLeft-1;
		}else{
			$intLeft=$intTmp;
			$intRight=$p_intGroupSize-$intLeft-1;
		}
		if($p_intCurrentPage+$intRight>$p_intTotalPage){
			$intLeft=$intRight+$p_intCurrentPage-$p_intTotalPage+$intLeft;
		}
		if($p_intCurrentPage-$intLeft<1){
			$intRight=$intLeft-$p_intCurrentPage+$intRight+1;
		}
		$intPageStart=$p_intCurrentPage-$intLeft;
		if($intPageStart<1){
			$intPageStart=1;
		}
		$intPageEnd=$p_intCurrentPage+$intRight;
		if($intPageEnd>$p_intTotalPage){
			$intPageEnd=$p_intTotalPage;
		}
		$intPageEnd=$intPageEnd+1;
		$arrURL=array();
		for($i=$intPageStart;$i<$intPageEnd;++$i){
			$arrURL[]=array( 
					'INDEX'=>$i,
					'URL'=>'' 
			);
		}
		return $arrURL;
	}
}